const APP = getApp()
const WXAPI = require('apifm-wxapi')
const UTILS = require('../../utils/util.js')
// fixed首次打开不显示标题的bug
APP.configLoadOK = () => {
  
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    maxDate:new Date().getTime(),
    currentMonth: new Date().getTime(),
    currentMonthText:"",
    accountLogs:[]
  },
  onLoad: function (options) {
    const currentMonthText = UTILS.dateFormat("yyyy-MM")
    this.setData({
      currentMonthText
    })
    this.accountLogs()
  },
  onShow: function () {

  },
  onSelectMonth(event){
    const currentMonth = event.detail
    const currentMonthText = UTILS.dateFormat("yyyy-MM",new Date(currentMonth))
    this.setData({
      currentMonth,currentMonthText
    })
    this.selectComponent('#time-dropdown').toggle();
     this.accountLogs()
  },
  async accountLogs() {
    wx.showLoading({
      title: '',
    })
    const res = await WXAPI.accountLogs({
    month:this.data.currentMonthText
    })
    wx.hideLoading()
 
    if (res.code == 0) {
      const accountLogs = res.data.map(item=>{
        item.amount = item.amount >=0 ? '+￥'+item.amount : '-￥'+ Math.abs(item.amount )
        return item
      })
      this.setData({
        accountLogs: res.data
      })
    }
  },
})